<?php

namespace app\admin\controller;

use hg\apidoc\annotation as Apidoc;
use hg\apidoc\annotation\Param;
use hg\apidoc\annotation\Query;
use hg\apidoc\annotation\Returned;
use hg\apidoc\annotation\Header;
use app\BaseController;
use think\facade\View;
use think\facade\Db;
use think\response\Json;
/**
 * apikey库
 *
 * admin-ApikeyLibrary
 *
 * @Apidoc\Title("apikey库")
 * @Apidoc\Group("api")
 */
class ApikeyLibrary extends BaseController
{
    /**
     * 新增
     *
     * admin-ApikeyLibrary-add
     */
    public function add()
    {
        if (request()->isPost()) {
            $data = input();
            $keys=explode("\n",$data['apikey_library__name']);
            foreach($keys as $k){
                $keyval=trim($k);
                if(!$keyval){
                    continue;
                }
                $savedata=[
                    "name"=>$keyval,
                    "belonging_account"=>$data['apikey_library__belonging_account'],
                    "pubid"=>$data['apikey_library__pubid'],
                ];
                \app\common\model\ApikeyLibrary::create($savedata);                
            }
            return success();
        } else {
            return View::fetch();
        }
    }
    /**
     * 修改
     *
     * admin-ApikeyLibrary-update
     */
    public function update()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\ApikeyLibrary::update($data['apikey_library']);
            return success();
        } else {
            $data = \app\common\model\ApikeyLibrary::find(input('apikey_library__id'));
            $r = [];
            foreach ($data->getData() as $key => $val) {
                $r['apikey_library__' . $key] = $val;
            }
            View::assign('r', $r);
            return View::fetch();
        }
    }
    /**
     * 删除
     *
     * admin-ApikeyLibrary-delete
     */
    public function delete()
    {
        \app\common\model\ApikeyLibrary::destroy(input('apikey_library__id'));
        return success();
    }
    /**
     * 列表
     *
     * admin-ApikeyLibrary-list
     */
    public function list()
    {
        if (request()->isPost()) {
            return error('请求方式错误');
        }
        $query = Db::table('db_apikey_library');
        if (input('apikey_library__name', '') !== '') {
            $query->where('db_apikey_library.name', 'like', '%' . input('apikey_library__name', '', 'search_in') . '%');
        }
        if (input('apikey_library__state', '') !== '') {
            $query->where('db_apikey_library.state', '=', input('apikey_library__state', '', 'search_in'));
        }
        $query->where('db_apikey_library.delete_time', '=', null);
        $query->field(['db_apikey_library.id' => 'apikey_library__id']);
        $query->field(['db_apikey_library.name' => 'apikey_library__name']);
        $query->field(['db_apikey_library.state' => 'apikey_library__state']);
        $query->field(['db_apikey_library.usage_time' => 'apikey_library__usage_time']);
        $query->withAttr('apikey_library__usage_time', function ($value, $data) {
            return $value?date('Y-m-d H:i:s', $value):'-';
        });
        $query->field(['db_apikey_library.end_time' => 'apikey_library__end_time']);
        $query->withAttr('apikey_library__end_time', function ($value, $data) {
            return $value?date('Y-m-d H:i:s', $value):'-';
        });
        $query->field(['db_apikey_library.end_reason' => 'apikey_library__end_reason']);
        $query->field(['db_apikey_library.belonging_account' => 'apikey_library__belonging_account']);
        $query->order('db_apikey_library.id', 'desc');
        $data = $query->paginate(input('listRow') ?: 15);
        $data = ['data' => $data->items(), 'paginate' => $data->render()];
        View::assign($data);
        $options = [];
        $apikey_library__state_option = [['name' => '未使用', 'id' => '0'], ['name' => '使用中', 'id' => '1'], ['name' => '临时下架', 'id' => '2'], ['name' => '已报废', 'id' => '3']];
        $options['apikey_library__state_options'] = $apikey_library__state_option;
        $apikey_library__id_option = \app\common\model\ApikeyLibrary::column('name as name, id as id');
        $options['apikey_library__id_options'] = $apikey_library__id_option;
        $apikey_library__state_option = [['name' => '未使用', 'id' => '0'], ['name' => '使用中', 'id' => '1'], ['name' => '临时下架', 'id' => '2'], ['name' => '已报废', 'id' => '3']];
        $options['apikey_library__state_options'] = $apikey_library__state_option;
        View::assign($options);
        return View::fetch();
    }
}
